class Monster{
	constructor(){
		this.width = 20
		this.color = 'red'
		this.left = 0
		this.top = 0
		this.birthPosition = []  //怪物出生位置数组
		this.monsterArr = []  //怪物数组
		this.monsterInterval = 200  //怪物移动间隔时间
	}
	
	// parseInt(Math.random() * 50)
	
	// 怪物出生位置数组
	createMonsterBirthPosition() {
		for (let i = 0; i < 500; i++) {
			if (i % 10 === 0) {
				this.birthPosition.push(i)
			}
		}
		//console.log('birthPosition',this.birthPosition);
	}
	
	//生成怪物
	createMonster(){
		const positonIndex = parseInt(Math.random() * 50)
		const left = this.birthPosition[positonIndex]
		const top = this.top
		const monster = document.createElement('div')
		
		this.monsterArr.push({
			left: left,
			top: top,
			node: monster
		})
		this.drawMonster()
	}
	//画怪物
	drawMonster(){
		for(let i = 0; i<this.monsterArr.length; i++){
			const node = this.monsterArr[i].node
			node.style.width = this.width + 'px'
			node.style.height = this.width + 'px'
			node.style.background = 'url(./image/monster.png)'
			node.style.backgroundSize = '20px 20px'
			node.style.position = 'absolute'
			node.style.left = this.monsterArr[i].left + 'px'
			node.style.top = this.monsterArr[i].top + 'px'
			map.appendChild(node)
		}
		
	}
	
	//清除怪物
	clearMonster() {
		for (let i = 0; i < this.monsterArr.length; i++) {
			if (this.monsterArr[i].node) {
				map.removeChild(this.monsterArr[i].node)
			}
		}
	}
	
	//怪物移动
	monsterMove(){
		setInterval(() =>{
			for(let i = 0; i<this.monsterArr.length;i++){
				this.monsterArr[i].top+=this.width
				if(this.monsterArr[i].top==800){
					plane.blood -= 1
					if(plane.blood <= 0 ) alert('食屎拉雷')
					map.removeChild(this.monsterArr[i].node)
					this.monsterArr.splice(i, 1)
				}
			}
			this.clearMonster()
			this.drawMonster()
		},this.monsterInterval)
		
	}
}